public class CountAndSay {
    public String countAndSay(int n) {
        if(n == 1) {
            // 递归出口
            return "1";
        }

        String RLE = countAndSay(n-1);

        char[] ch = RLE.toCharArray();
        int len = ch.length;
        int i = 0;

        StringBuilder ret = new StringBuilder();
        while(i < len) {
            int repeat = 0;
            char pivot = ch[i]; // 将遇见的第一个不同种数字作为基准值
            while(i < len && ch[i] == pivot) {
                repeat++;
                i++;
            }
            ret.append((char)(repeat + '0'));
            ret.append(pivot);
        }
        return ret.toString();
    }
}
